
#  KCChess -- yet another xboard-compatible chess engine.
#  Copyright (C) 2006-2007 Konstantin 'KCC' Serebryany <konstantin.s.serebryany#gmail.com>

#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#  
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#  
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.

CXX       = g++
OPT       = -O2 -static
OPENMP    = #-fopenmp
ARCH      = x86
THREADS   =
DEBUG     = 0
EXE       = ./a.exe
PG        = 
EPD       = wac
DEPTH     = 9
TT_BITS   = 23
MAX_TIME  = 5

JTHREAD_DIR=./jthread_$(ARCH)

JTHREAD_LIBS = -L${JTHREAD_DIR}/lib -ljthread -lpthread
JTHREAD_INC  = -I${JTHREAD_DIR}/include


CXXFLAGS_ICC=-Wall -wd981,383,271,810
CXXFLAGS_GCC=-Wall
CXXFLAGS_ICL= -DMSWIN -Zc:forScope 

# compiler 
ifeq ($(CXX), icpc)
    CXXFLAGS += $(CXXFLAGS_ICC)
endif

ifeq ($(CXX), g++)
    CXXFLAGS += $(CXXFLAGS_GCC)
endif

ifeq ($(CXX), icl)
    CXXFLAGS += $(CXXFLAGS_ICL)
endif

# ARCH
ifeq ($(ARCH), x86)
    BITS=-DHAVE_x86_asm
endif

ifeq ($(ARCH), x86_64)
    BITS=-DHAVE_x86_64_asm
endif

ifeq ($(ARCH), ia64)
    BITS=-DHAVE_m64_popcnt
endif

# THREADS
ifeq ($(THREADS), JTHREAD)
    CXXFLAGS += $(JTHREAD_INC)
    LDFLAGS  += $(JTHREAD_LIBS)
endif

# debug
CXXFLAGS += -DDEBUG=$(DEBUG) -g
#ifeq ($(DEBUG), 1)
#    CXXFLAGS += -g
#endif


$(EXE): kcchess.cc kcchess.h Makefile
	@$(CXX) $(CXXFLAGS) $(OPT) $(OPENMP)  $(BITS) $(PG) kcchess.cc -DKCChess_main=main $(LDFLAGS) -o $(EXE)
	@$(MAKE) SANITY
SANITY: perftsuite.epd
	@head -30 wac_mate.epd | $(EXE) --max_depth 9 epd - | grep FAILED | cat ;
	@$(EXE) --max_depth 4 perft perftsuite.epd > /dev/null

PG: clean
	$(MAKE) EXE=./pg.exe THREADS= DEBUG=0 PG=-pg
	$(MAKE) EXE=./pg.exe TEST
	gprof ./pg.exe | head -20
PG1: clean
	$(MAKE) EXE=./pg.exe THREADS= DEBUG=0 PG=-pg
	$(MAKE) EXE=./pg.exe TRAIN
	gprof ./pg.exe | head -20

BEST_ICPC: clean
	@$(MAKE) CXX=icpc DEBUG=0 OPT=-O2 TEST
BEST_PG: clean
	@$(MAKE) CXX=g++ DEBUG=0 OPT=-O2 PG

BENCH:
	@$(EXE) --max_depth 7 perft startpos 

COV: clean
	$(MAKE) CXX=g++ DEBUG=0 OPT="-O2 --coverage"
	$(MAKE) TRAIN
	gcov kcchess.cc

TEST: 
	@$(EXE) -j 0 --max_time 1 --max_depth 7 --tt_bits 16 epd $(EPD).epd | tee 7.log | tail -1

TRAIN: 
	@$(EXE) -j 0 --max_time 2 --max_depth 8 --tt_bits 18 epd $(EPD).epd | tee 8.log | tail -1

REF:
	@$(EXE) -j 0 --max_time $(MAX_TIME) --max_depth $(DEPTH) --tt_bits $(TT_BITS) epd $(EPD).epd | tee $(DEPTH).log | tail -1

clean:
	@rm -f a.exe pg.exe vt.exe pf.exe a.out core* gmon.out *.log log.* game.* *.gcov *.gcda *.gcno

RECORD:
	make TEST TRAIN REF| grep PASS.RATE | tee -a record

perftsuite.epd: 
	wget http://hem.passagen.se/maragor/perftsuite.epd
wac.epd:
	wget http://wannabe.guru.org/scott/hobbies/chess/WAC.EPD && mv WAC.EPD wac.epd
ecm.epd: 
	wget http://wannabe.guru.org/scott/hobbies/chess/ECM.EPD && mv ECM.EPD ecm.epd
wcsac.epd:
	wget http://wannabe.guru.org/scott/hobbies/chess/WCSAC.EPD && mv WCSAC.EPD wcsac.epd 

all_epd: wac.epd ecm.epd wcsac.epd perftsuite.epd

src=kcchess.cc kcchess.h Makefile
epd=wac.epd sanity.epd

other_files=prof
all_files=$(src) $(epd) $(other_files)

bak: $(all_files)
	tar zcf `date +%y.%b.%d.%H.%M.tgz`  $(all_files)
